<?php

require_once(SERVER_ROOT . '/daos/' . 'ProcessoDAO.php');
require_once(SERVER_ROOT . '/daos/' . 'ClienteDAO.php');
require_once (SERVER_ROOT . '/authenticate/' . 'Authenticator.php');

class ProcessoController {

    private $processoDAO;
    private $clienteDAO;
    private $usuario;

    public function __construct() {
        $this->processoDAO = new ProcessoDAO();
        $this->clienteDAO = new ClienteDAO();
        $this->usuario = Authenticator::getUser();
    }

    public function salvar(array $params) {
        if (empty($params["tipo_acao"])) {
            $tipo_acao = "";
        } else {
            $tipo_acao = $params["tipo_acao"];
        }
        if (empty($params["parte_contraria"])) {
            $parte_contraria = "";
        } else {
            $parte_contraria = $params["parte_contraria"];
        }
        if (empty($params["vara"])) {
            $vara = "";
        } else {
            $vara = $params["vara"];
        }
        if (empty($params["num_processo"])) {
            $num_processo = "";
        } else {
            $num_processo = $params["num_processo"];
        }
        $data = array(
            'id_usuario' => $this->usuario->id_usuario, 'id_escritorio' => $this->usuario->id_escritorio, 'tipo_acao' => $tipo_acao, 'parte_contraria' => $parte_contraria, 'vara' => $vara, 'num_processo' => $num_processo, 'status' => 1, 'advogado' => $params["advogado"], 'secretaria' => $params["secretaria"], 'estagiario' => $params["estagiario"]);
        $result = $this->processoDAO->save($data);
        //retorno 1 caso salve o cliente ou 0 para erro
        echo json_encode($result);
    }

    public function getProcesso(array $params) {

        $row = $this->processoDAO->findById($params['id_processo']);
        $response = array(
            $row['id_processo'],
            $row['tipo_acao'],
            $row['parte_contraria'],
            $row['vara'],
            $row['num_processo'],
            $row['status']
        );
        echo json_encode($response);
    }

    public function atualizarProcesso(array $params) {
        $rows = $this->processoDAO->update($params);
        if ($rows) {
            echo "ok";
        } else {
            echo "fail";
        }
    }

    public function ativarProcesso(array $params) {
        $this->processoDAO->activateProcessoById($params['id_processo']);
    }

    public function inativarProcesso(array $params) {
        $this->processoDAO->inactivateProcessoById($params['id_processo']);
    }

    public function removerProcesso(array $params) {
        $result = $this->processoDAO->removeById($params['id_processo']);
        if ($result == 0) {
            echo "ok";
        }
        echo "fail";
    }

    public function listAllByUsuario($p) {
        $page = $p['page'];
        $limit = $p['rows'];
        $sidx = $p['sidx'];
        $sord = $p['sord'];
        $search_field = $p['searchField'];
        $search_string = $p['searchString'];
        $search_oper = $p['searchOper'];
        $id_usuario = $this->usuario->id_usuario;
        if (!$sidx)
            $sidx = 1;
        $count = $this->processoDAO->count();
        $total_pages = ($count > 0 ? ceil($count / $limit) : $total_pages = 0);
        if ($page > $total_pages)
            $page = $total_pages;
        $start = $limit * $page - $limit;
        if ($start < 0)
            $start = 0;
        if ($search_string == "") {
            $result = $this->processoDAO->findAllByUsuario($id_usuario, $sidx, $sord, $start, $limit);
        } else {
            $result = $this->processoDAO->findAllByUsuarioWithFilters($id_usuario, $sidx, $sord, $start, $limit, $search_field, $search_string, $search_oper);
        }

        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $count;
        for ($i = 0; $i < count($result); $i++) {
            $row = $result[$i];
            $nome_clientes = $this->processoDAO->findAllNomesClientesByIdProcesso($row['id_processo']);
            $response->rows[$i]['id'] = $row['id_processo'];
            $response->rows[$i]['cell'] = array($row['id_processo'], $row['tipo_acao'], $row['parte_contraria'], $row['vara'], $row['num_processo'], $nome_clientes, $row['status'], '');
        }
        echo json_encode($response);
    }

    public function listAllProcesso($p) {
        $page = $p['page'];
        $limit = $p['rows'];
        $sidx = $p['sidx'];
        $sord = $p['sord'];
        $search_field = $p['searchField'];
        $search_string = $p['searchString'];
        $search_oper = $p['searchOper'];
        if (!$sidx)
            $sidx = 1;
        $count = $this->processoDAO->count();
        $total_pages = ($count > 0 ? ceil($count / $limit) : $total_pages = 0);
        if ($page > $total_pages)
            $page = $total_pages;
        $start = $limit * $page - $limit;
        if ($start < 0)
            $start = 0;
        if ($search_string == "") {
            $result = $this->processoDAO->findAll($sidx, $sord, $start, $limit);
        } else {
            $result = $this->processoDAO->findAllWithFilters($sidx, $sord, $start, $limit, $search_field, $search_string, $search_oper);
        }

        $response->total = $total_pages;
        $response->records = $count;
        for ($i = 0; $i < count($result); $i++) {
            $row = $result[$i];
            $nome_clientes = $this->processoDAO->findAllNomesClientesByIdProcesso($row['id_processo']);
            $response->rows[$i]['id'] = $row['id_processo'];
            $response->rows[$i]['cell'] = array($row['id_processo'], $row['tipo_acao'], $row['parte_contraria'], $row['vara'], $row['num_processo'], $nome_clientes, $row['status'], '');
        }
        echo json_encode($response);
    }

}